//题目链接：https://www.nowcoder.com/practice/a55a584bc0ca4a83a272680174be113b?tpId=196&tqId=37151&ru=/exam/oj
const int N = 1e6 + 10;
int son[N][26] , cnt[N] , pre[N],idx;

class Solution {
public:
    void insert(const char str[])
    {
        int p = 0;
        for(int i = 0 ; str[i] ; ++i)
        {
            int u = str[i] - 'a';
            if(!son[p][u])
            {
                son[p][u] = ++idx;
            }
            p = son[p][u];
            pre[p]++;
        }
        cnt[p]++;
    }
    void del(const char str[])
    {
        int p = 0;
        for(int i = 0 ; str[i] ; ++i)
        {
            int u = str[i] - 'a';
            p = son[p][u];
            pre[p]--;
        }
        cnt[p]--;
    }
    bool query(const char str[])
    {
        int p = 0;
        int tmp = cnt[str[0]-'a'];
        for(int i = 0 ; str[i] ; ++i)
        {
            int u = str[i] - 'a';
            if(!son[p][u]) return false;
            p = son[p][u];
        }
        return cnt[p];
    }
    int prefixNumber(const char str[])
    {
        int p = 0;
        for(int i = 0 ; str[i] ; ++i)
        {
            int u = str[i] - 'a';
            if(!son[p][u]) return 0;
            p = son[p][u];
        }
        return pre[p];
    }
    vector<string> trieU(vector<vector<string> >& operators) {
        vector<string> ret;
        for(auto& iter : operators)
        {
            string op = iter[0];
            string str = iter[1];
            if(op == "1") insert(str.c_str());
            else if(op == "2") del(str.c_str());
            else if(op == "3")
            {
                bool res = query(str.c_str());
                if(res) ret.push_back("YES");
                else ret.push_back("NO");
            }
            else if(op == "4")
            {
                int num = prefixNumber(str.c_str());
                ret.push_back(to_string(num));
            }
        }
        return ret;
    }
};